package com.zyong.design.pattern.mediator.case2;

import java.util.HashMap;

/**
 * 具体的中介者类
 * @program: zyong-design-pattern
 * @ClassName: ConcreteMediator
 * @author: zyong
 * @create: 2024-04-02 23:56
 */
public class ConcreteMediator implements Mediator {
    /**
     * colleagueMap和interMap的设计巧妙之处
     * interMap     学名，别名
     * colleagueMap 别名, 实例对象
     * interMap和colleagueMap    学名, 实例对象
     * 方便根据学名，定位获取到实例对象
     */
    /**
     * 集合，加入所有的同事类对象
     */
    private HashMap<String, Colleague> colleagueMap = new HashMap<>();
    private HashMap<String, String> interMap = new HashMap<>();

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);

        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        } else if (colleague instanceof Tv) {
            interMap.put("TV", colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String colleagueName) {
        Colleague colleague = colleagueMap.get(colleagueName);
        if (colleague instanceof Alarm) {
            String coffeeMachine = interMap.get("CoffeeMachine");
            String tv = interMap.get("TV");
            if (stateChange == 0) {
                ((CoffeeMachine) colleagueMap.get(coffeeMachine)).startCoffee();
                ((Tv) colleagueMap.get(tv)).startTv();
            } else if (stateChange == 1) {
                ((Tv) colleagueMap.get(tv)).stopTv();
            }
        } else if (colleague instanceof CoffeeMachine) {
            String curtains = interMap.get("Curtains");
            ((Curtains) colleagueMap.get(curtains)).upCurtains();
        } else if (colleague instanceof Curtains) {

        } else if (colleague instanceof Tv) {

        }
    }

    @Override
    public void sendMessage() {
        for (Colleague colleague : colleagueMap.values()) {
            colleague.sendMessage(0);
        }
    }
}
